﻿namespace TestTool.GUI.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Xml;
    using System.Xml.Serialization;
    using TestTool.GUI;
    using TestTool.GUI.Data;
    using TestTool.GUI.Utils;
    using TestTool.GUI.Views;
    using TestTool.Proxies.Onvif;
    using TestTool.Tests.Common.CommonUtils;
    using TestTool.Tests.Definitions.Interfaces;
    using TestTool.Tests.Definitions.UI;

    internal class ManagementController : Controller<IManagementView>
    {
        private Dictionary<string, Type> _advancedSettingsTypes;
        private List<string> _operations;
        private ProfilesSet _profiles;
        public const string EVENTSSETTINGS = "EventTopic";

        public event Action<TestTool.GUI.Data.Profile> ProfileApplied;

        public event EventHandler SettingsLoaded;

        public ManagementController(IManagementView view) : base(view)
        {
            this._profiles = new ProfilesSet();
        }

        public void AddSettingsPages(List<SettingsTabPage> pages)
        {
            this._advancedSettingsTypes = new Dictionary<string, Type>();
            foreach (SettingsTabPage page in pages)
            {
                Type parametersType = page.ParametersType;
                this._advancedSettingsTypes.Add(parametersType.Name, parametersType);
            }
            base.View.SettingsView.AddSettingsPages(pages);
        }

        public void ApplyProfile(TestTool.GUI.Data.Profile profile)
        {
            this.UpdateContext();
            this.DisplayAdvancedSettings(profile.AdvancedSettings);
            if (this.ProfileApplied != null)
            {
                this.ProfileApplied(profile);
            }
        }

        private void DisplayAdvancedSettings(List<XmlElement> elements)
        {
            if (elements == null)
            {
                base.View.SettingsView.AdvancedSettings = null;
            }
            else
            {
                List<object> list = this.ParseAdvancedSettings(elements);
                base.View.SettingsView.AdvancedSettings = list;
            }
        }

        private List<XmlElement> GetAdvancedSettings()
        {
            return AdvancedParametersUtils.Serialize(base.View.SettingsView.AdvancedSettings);
        }

        public void GetPTZNodes()
        {
            DiscoveredDevices discoveredDevices = ContextController.GetDiscoveredDevices();
            string str = (discoveredDevices != null) ? discoveredDevices.ServiceAddress : string.Empty;
            DeviceEnvironment env = ContextController.GetDeviceEnvironment();
            ManagementServiceProvider deviceClient = new ManagementServiceProvider(str, env.Timeouts.Message);
            base.ReportOperationStarted();
            new Thread(new ThreadStart(new Action(delegate {
                try
                {
                    DeviceServicesInfo capabilitiesDefineSecurity = deviceClient.GetCapabilitiesDefineSecurity(new CapabilityCategory[] { CapabilityCategory.PTZ });
                    Capabilities capabilities = capabilitiesDefineSecurity.Capabilities;
                    string xAddr = string.Empty;
                    if (capabilities != null)
                    {
                        if (capabilities.PTZ == null)
                        {
                            throw new Exception("Device does not support PTZ service");
                        }
                        xAddr = capabilities.PTZ.XAddr;
                    }
                    else
                    {
                        if (capabilitiesDefineSecurity.Services == null)
                        {
                            throw new Exception("Unable to get service address");
                        }
                        Service service = capabilitiesDefineSecurity.Services.FindService("http://www.onvif.org/ver20/ptz/wsdl");
                        if (service == null)
                        {
                            throw new Exception("Device does not support PTZ service");
                        }
                        xAddr = service.XAddr;
                    }
                    PTZNode[] nodes = new PTZServiceProvider(xAddr, env.Timeouts.Message) { Security = deviceClient.Security }.GetNodes();
                    if ((nodes == null) || (nodes.Length <= 0))
                    {
                        throw new Exception("No PTZ nodes returned by device");
                    }
                    this.View.SettingsView.SetPTZNodes(nodes);
                }
                catch (Exception exception)
                {
                    this.View.ShowError(exception);
                }
                finally
                {
                    this.ReportOperationCompleted();
                }
            }).Invoke)) { CurrentUICulture = CultureInfo.InvariantCulture }.Start();
        }

        public override void Initialize()
        {
            base.Initialize();
            base.View.DisplayProfiles(this.Load());
        }

        public List<TestTool.GUI.Data.Profile> Load()
        {
            this._profiles = ProfilesManager.Load();
            return this._profiles.Profiles;
        }

        public override void LoadSavedContext(SavedContext context)
        {
            if (context.DeviceEnvironment != null)
            {
                if (context.DeviceEnvironment.Timeouts != null)
                {
                    base.View.SettingsView.MessageTimeout = context.DeviceEnvironment.Timeouts.Message;
                    base.View.SettingsView.RebootTimeout = context.DeviceEnvironment.Timeouts.Reboot;
                    base.View.SettingsView.TimeBetweenTests = context.DeviceEnvironment.Timeouts.InterTests;
                }
                if (context.DeviceEnvironment.EnvironmentSettings != null)
                {
                    base.View.SettingsView.DnsIpv4 = context.DeviceEnvironment.EnvironmentSettings.DnsIpv4;
                    base.View.SettingsView.DnsIpv6 = context.DeviceEnvironment.EnvironmentSettings.DnsIpv6;
                    base.View.SettingsView.NtpIpv4 = context.DeviceEnvironment.EnvironmentSettings.NtpIpv4;
                    base.View.SettingsView.NtpIpv6 = context.DeviceEnvironment.EnvironmentSettings.NtpIpv6;
                    base.View.SettingsView.GatewayIpv4 = context.DeviceEnvironment.EnvironmentSettings.GatewayIpv4;
                    base.View.SettingsView.GatewayIpv6 = context.DeviceEnvironment.EnvironmentSettings.GatewayIpv6;
                }
                if (context.DeviceEnvironment.TestSettings != null)
                {
                    base.View.SettingsView.PTZNodeToken = context.DeviceEnvironment.TestSettings.PTZNodeToken;
                    base.View.SettingsView.VideoSourceToken = context.DeviceEnvironment.TestSettings.VideoSourceToken;
                    base.View.SettingsView.UseEmbeddedPassword = context.DeviceEnvironment.TestSettings.UseEmbeddedPassword;
                    base.View.SettingsView.Password1 = context.DeviceEnvironment.TestSettings.Password1;
                    base.View.SettingsView.Password2 = context.DeviceEnvironment.TestSettings.Password2;
                    base.View.SettingsView.SecureMethod = context.DeviceEnvironment.TestSettings.SecureMethod;
                    base.View.SettingsView.OperationDelay = context.DeviceEnvironment.TestSettings.OperationDelay;
                    base.View.SettingsView.RecoveryDelay = context.DeviceEnvironment.TestSettings.RecoveryDelay;
                    base.View.SettingsView.SubscriptionTimeout = context.DeviceEnvironment.TestSettings.SubscriptionTimeout;
                    base.View.SettingsView.EventTopic = context.DeviceEnvironment.TestSettings.EventTopic;
                    string str = context.DeviceEnvironment.TestSettings.TopicNamespaces.Replace("\n", Environment.NewLine);
                    context.DeviceEnvironment.TestSettings.TopicNamespaces = str;
                    base.View.SettingsView.TopicNamespaces = context.DeviceEnvironment.TestSettings.TopicNamespaces;
                    base.View.SettingsView.RecordingToken = context.DeviceEnvironment.TestSettings.RecordingToken;
                    base.View.SettingsView.SearchTimeout = context.DeviceEnvironment.TestSettings.SearchTimeout;
                    base.View.SettingsView.MetadataFilter = context.DeviceEnvironment.TestSettings.MetadataFilter;
                    if (string.IsNullOrEmpty(context.DeviceEnvironment.TestSettings.RetentionTime))
                    {
                        base.View.SettingsView.RetentionTime = "P1D";
                    }
                    else
                    {
                        base.View.SettingsView.RetentionTime = context.DeviceEnvironment.TestSettings.RetentionTime;
                    }
                    if (context.DeviceEnvironment.TestSettings.RelayOutputDelayTimeMonostable > 0)
                    {
                        base.View.SettingsView.RelayOutputDelayTimeMonostable = context.DeviceEnvironment.TestSettings.RelayOutputDelayTimeMonostable;
                    }
                    this.DisplayAdvancedSettings(context.DeviceEnvironment.TestSettings.RawAdvancedSettings);
                    ContextController.GetDeviceEnvironment().TestSettings.AdvancedSettings = this.ParseAdvancedSettings(context.DeviceEnvironment.TestSettings.RawAdvancedSettings);
                }
            }
        }

        public void LoadSettingsFromXML(string fileName)
        {
            try
            {
                SerializableTestingParameters parameters = null;
                if (!File.Exists(fileName))
                {
                    base.View.ShowError(string.Format("File not found: {0}", fileName));
                }
                else
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(SerializableTestingParameters));
                    FileStream stream = null;
                    try
                    {
                        stream = new FileStream(fileName, FileMode.Open);
                        parameters = (SerializableTestingParameters) serializer.Deserialize(stream);
                    }
                    catch (Exception exception)
                    {
                        base.View.ShowError(exception);
                        base.View.ShowError(string.Format("Parameters loading failed: {0}", exception.Message));
                        return;
                    }
                    finally
                    {
                        if (stream != null)
                        {
                            stream.Close();
                        }
                    }
                    DeviceEnvironment deviceEnvironment = ContextController.GetDeviceEnvironment();
                    if (parameters.Advanced != null)
                    {
                        deviceEnvironment.TestSettings.AdvancedSettings = this.ParseAdvancedSettings(parameters.Advanced);
                        deviceEnvironment.TestSettings.RawAdvancedSettings = new List<XmlElement>(parameters.Advanced);
                    }
                    if (parameters.Device != null)
                    {
                        ContextController.GetDiscoveredDevices().ServiceAddress = parameters.Device.DeviceServiceAddress;
                    }
                    SessionInfo sessionInfo = parameters.SessionInfo;
                    if (parameters.TestParameters != null)
                    {
                        TestParameters testParameters = parameters.TestParameters;
                        deviceEnvironment.Credentials.UserName = testParameters.UserName;
                        deviceEnvironment.Credentials.Password = testParameters.Password;
                        deviceEnvironment.EnvironmentSettings.DnsIpv4 = testParameters.DnsIpv4;
                        deviceEnvironment.EnvironmentSettings.DnsIpv6 = testParameters.DnsIpv6;
                        deviceEnvironment.EnvironmentSettings.GatewayIpv4 = testParameters.DefaultGatewayIpv4;
                        deviceEnvironment.EnvironmentSettings.GatewayIpv6 = testParameters.DefaultGatewayIpv6;
                        deviceEnvironment.EnvironmentSettings.NtpIpv4 = testParameters.NtpIpv4;
                        deviceEnvironment.EnvironmentSettings.NtpIpv6 = testParameters.NtpIpv6;
                        deviceEnvironment.TestSettings.EventTopic = testParameters.EventTopic;
                        deviceEnvironment.TestSettings.MetadataFilter = testParameters.MetadataFilter;
                        deviceEnvironment.TestSettings.OperationDelay = testParameters.OperationDelay;
                        deviceEnvironment.TestSettings.Password1 = testParameters.Password1;
                        deviceEnvironment.TestSettings.Password2 = testParameters.Password2;
                        deviceEnvironment.TestSettings.PTZNodeToken = testParameters.PTZNodeToken;
                        deviceEnvironment.TestSettings.RecordingToken = testParameters.RecordingToken;
                        deviceEnvironment.TestSettings.RecoveryDelay = testParameters.TimeBetweenRequests;
                        deviceEnvironment.TestSettings.RelayOutputDelayTimeMonostable = testParameters.RelayOutputDelayTime;
                        deviceEnvironment.TestSettings.SearchTimeout = testParameters.SearchTimeout;
                        deviceEnvironment.TestSettings.SecureMethod = testParameters.SecureMethod;
                        deviceEnvironment.TestSettings.SubscriptionTimeout = testParameters.SubscriptionTimeout;
                        deviceEnvironment.TestSettings.TopicNamespaces = testParameters.TopicNamespaces;
                        deviceEnvironment.TestSettings.UseEmbeddedPassword = testParameters.UseEmbeddedPassword;
                        deviceEnvironment.TestSettings.VideoSourceToken = testParameters.VideoSourceToken;
                        deviceEnvironment.Timeouts.InterTests = testParameters.TimeBetweenTests;
                        deviceEnvironment.Timeouts.Message = testParameters.MessageTimeout;
                        deviceEnvironment.Timeouts.Reboot = testParameters.RebootTimeout;
                    }
                    this.UpdateView();
                    if (this.SettingsLoaded != null)
                    {
                        this.SettingsLoaded(this, new EventArgs());
                    }
                }
            }
            catch (Exception exception2)
            {
                base.View.ShowError(exception2);
            }
        }

        public void OnPostLoadContextData()
        {
            Timeouts timeouts = new Timeouts();
            if (base.View.SettingsView.MessageTimeout == 0)
            {
                base.View.SettingsView.MessageTimeout = timeouts.Message;
            }
            if (base.View.SettingsView.RebootTimeout == 0)
            {
                base.View.SettingsView.RebootTimeout = timeouts.Reboot;
            }
            if (base.View.SettingsView.TimeBetweenTests == 0)
            {
                base.View.SettingsView.TimeBetweenTests = timeouts.InterTests;
            }
            if (string.IsNullOrEmpty(base.View.SettingsView.DnsIpv4))
            {
                base.View.SettingsView.DnsIpv4 = "10.1.1.1";
            }
            if (string.IsNullOrEmpty(base.View.SettingsView.NtpIpv4))
            {
                base.View.SettingsView.NtpIpv4 = "10.1.1.1";
            }
            if (string.IsNullOrEmpty(base.View.SettingsView.GatewayIpv4))
            {
                base.View.SettingsView.GatewayIpv4 = "10.1.1.1";
            }
            if (string.IsNullOrEmpty(base.View.SettingsView.DnsIpv6))
            {
                base.View.SettingsView.DnsIpv6 = "2001:1:1:1:1:1:1:1";
            }
            if (string.IsNullOrEmpty(base.View.SettingsView.NtpIpv6))
            {
                base.View.SettingsView.NtpIpv6 = "2001:1:1:1:1:1:1:1";
            }
            if (string.IsNullOrEmpty(base.View.SettingsView.GatewayIpv6))
            {
                base.View.SettingsView.GatewayIpv6 = "2001:1:1:1:1:1:1:1";
            }
            if (base.View.SettingsView.SubscriptionTimeout == 0)
            {
                base.View.SettingsView.SubscriptionTimeout = 60;
            }
            if (base.View.SettingsView.RelayOutputDelayTimeMonostable == 0)
            {
                base.View.SettingsView.RelayOutputDelayTimeMonostable = 20;
            }
            if (base.View.SettingsView.SearchTimeout == 0)
            {
                base.View.SettingsView.SearchTimeout = 10;
            }
            this.UpdateContext();
        }

        private List<object> ParseAdvancedSettings(IEnumerable<XmlElement> elements)
        {
            return AdvancedParametersUtils.Deserialize(elements, this._advancedSettingsTypes.Values);
        }

        public void QueryEventTopics()
        {
            DiscoveredDevices discoveredDevices = ContextController.GetDiscoveredDevices();
            string str = (discoveredDevices != null) ? discoveredDevices.ServiceAddress : string.Empty;
            DeviceEnvironment env = ContextController.GetDeviceEnvironment();
            ManagementServiceProvider deviceClient = new ManagementServiceProvider(str, env.Timeouts.Message);
            base.ReportOperationStarted();
            new Thread(new ThreadStart(new Action(delegate {
                try
                {
                    DeviceServicesInfo capabilitiesDefineSecurity = deviceClient.GetCapabilitiesDefineSecurity(new CapabilityCategory[] { CapabilityCategory.Events });
                    Capabilities capabilities = capabilitiesDefineSecurity.Capabilities;
                    string xAddr = string.Empty;
                    if (capabilities != null)
                    {
                        if (capabilities.Events == null)
                        {
                            throw new Exception("Device does not support Events service");
                        }
                        xAddr = capabilities.Events.XAddr;
                    }
                    else
                    {
                        if (capabilitiesDefineSecurity.Services == null)
                        {
                            throw new Exception("Unable to get service address");
                        }
                        Service service = capabilitiesDefineSecurity.Services.FindService("http://www.onvif.org/ver10/events/wsdl");
                        if (service == null)
                        {
                            throw new Exception("Device does not support Events service");
                        }
                        xAddr = service.XAddr;
                    }
                    List<EventsTopicInfo> topics = new EventsServiceProvider(xAddr, env.Timeouts.Message) { Security = deviceClient.Security }.GetTopics();
                    if ((topics == null) || (topics.Count <= 0))
                    {
                        throw new Exception("No Event Topics returned by device");
                    }
                    this.View.SettingsView.SetEventsTopic(topics);
                }
                catch (Exception exception)
                {
                    this.View.ShowError(exception);
                }
                finally
                {
                    this.ReportOperationCompleted();
                }
            }).Invoke)) { CurrentUICulture = CultureInfo.InvariantCulture }.Start();
        }

        public void QueryRecordings()
        {
            DiscoveredDevices discoveredDevices = ContextController.GetDiscoveredDevices();
            string serviceAddress = (discoveredDevices != null) ? discoveredDevices.ServiceAddress : string.Empty;
            DeviceEnvironment env = ContextController.GetDeviceEnvironment();
            ManagementServiceProvider deviceClient = new ManagementServiceProvider(serviceAddress, env.Timeouts.Message);
            base.ReportOperationStarted();
            new Thread(new ThreadStart(new Action(delegate {
                try
                {
                    DeviceServicesInfo capabilitiesDefineSecurity = deviceClient.GetCapabilitiesDefineSecurity(new CapabilityCategory[1]);
                    Capabilities capabilities = capabilitiesDefineSecurity.Capabilities;
                    string xAddr = string.Empty;
                    if (((capabilities != null) && (capabilities.Extension != null)) && (capabilities.Extension.Recording != null))
                    {
                        xAddr = capabilities.Extension.Recording.XAddr;
                    }
                    if (string.IsNullOrEmpty(xAddr))
                    {
                        if (capabilitiesDefineSecurity.Services == null)
                        {
                            throw new Exception("Unable to get service address");
                        }
                        Service service = capabilitiesDefineSecurity.Services.FindService("http://www.onvif.org/ver10/recording/wsdl");
                        if (service != null)
                        {
                            xAddr = service.XAddr;
                        }
                    }
                    if (string.IsNullOrEmpty(xAddr))
                    {
                        throw new Exception("Device does not support Recording service");
                    }
                    RecordingServiceProvider provider = new RecordingServiceProvider(xAddr, env.Timeouts.Message)
                    {
                        Security = deviceClient.Security
                    };
                    List<string> recordings = (from R in provider.GetRecordings() select R.RecordingToken).ToList<string>();
                    if ((recordings == null) || (recordings.Count <= 0))
                    {
                        throw new Exception("No Recordings returned by device");
                    }
                    this.View.SettingsView.SetRecordings(recordings);
                }
                catch (Exception exception)
                {
                    this.View.ShowError(exception);
                }
                finally
                {
                    this.ReportOperationCompleted();
                }
            }).Invoke)) { CurrentUICulture = CultureInfo.InvariantCulture }.Start();
        }

        public void QueryVideoSources()
        {
            DiscoveredDevices discoveredDevices = ContextController.GetDiscoveredDevices();
            string str = (discoveredDevices != null) ? discoveredDevices.ServiceAddress : string.Empty;
            DeviceEnvironment env = ContextController.GetDeviceEnvironment();
            ManagementServiceProvider deviceClient = new ManagementServiceProvider(str, env.Timeouts.Message);
            base.ReportOperationStarted();
            new Thread(new ThreadStart(new Action(delegate {
                try
                {
                    DeviceServicesInfo capabilitiesDefineSecurity = deviceClient.GetCapabilitiesDefineSecurity(new CapabilityCategory[] { CapabilityCategory.Media });
                    Capabilities capabilities = capabilitiesDefineSecurity.Capabilities;
                    string xAddr = string.Empty;
                    if (capabilities != null)
                    {
                        if (capabilities.Media == null)
                        {
                            throw new Exception("Device does not support Media service");
                        }
                        xAddr = capabilities.Media.XAddr;
                    }
                    else
                    {
                        if (capabilitiesDefineSecurity.Services == null)
                        {
                            throw new Exception("Unable to get service address");
                        }
                        Service service = capabilitiesDefineSecurity.Services.FindService("http://www.onvif.org/ver10/media/wsdl");
                        if (service == null)
                        {
                            throw new Exception("Device does not support Media service");
                        }
                        xAddr = service.XAddr;
                    }
                    VideoSource[] sources = new MediaServiceProvider(xAddr, env.Timeouts.Message) { Security = deviceClient.Security }.GetVideoSources();
                    if ((sources == null) || (sources.Length <= 0))
                    {
                        throw new Exception("No Video Sources returned by device");
                    }
                    this.View.SettingsView.SetVideoSources(sources);
                }
                catch (Exception exception)
                {
                    this.View.ShowError(exception);
                }
                finally
                {
                    this.ReportOperationCompleted();
                }
            }).Invoke)) { CurrentUICulture = CultureInfo.InvariantCulture }.Start();
        }

        public void SaveAsXML(string fileName)
        {
            SerializableTestingParameters o = new SerializableTestingParameters {
                Advanced = this.GetAdvancedSettings().ToArray(),
                Device = new DeviceParameters()
            };
            DiscoveredDevices discoveredDevices = ContextController.GetDiscoveredDevices();
            o.Device.DeviceIP = discoveredDevices.DeviceAddress.ToString();
            o.Device.DeviceServiceAddress = discoveredDevices.ServiceAddress;
            DeviceInfo devInfo = ContextController.GetSetupInfo().DevInfo;
            o.Device.Model = (devInfo != null) ? devInfo.Model : string.Empty;
            o.Output = new Output();
            o.Output.CreateNestedFolder = true;
            o.Output.Directory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            o.Output.FeatureDefinitionLog = "FeatureDefinitionLog.xml";
            o.Output.Report = "TestReport.pdf";
            o.Output.TestLog = "TestLog.pdf";
            SetupInfo setupInfo = ContextController.GetSetupInfo();
            if (setupInfo != null)
            {
                o.SessionInfo = new SessionInfo();
                o.SessionInfo.MemberInfo = setupInfo.MemberInfo;
                o.SessionInfo.OtherInformation = setupInfo.OtherInfo;
                o.SessionInfo.TesterInfo = setupInfo.TesterInfo;
            }
            o.TestParameters = new TestParameters();
            o.TestParameters.Address = discoveredDevices.NIC.IP.ToString();
            DeviceEnvironment deviceEnvironment = ContextController.GetDeviceEnvironment();
            if (deviceEnvironment.EnvironmentSettings != null)
            {
                o.TestParameters.DefaultGatewayIpv4 = deviceEnvironment.EnvironmentSettings.GatewayIpv4;
                o.TestParameters.DefaultGatewayIpv6 = deviceEnvironment.EnvironmentSettings.GatewayIpv6;
                o.TestParameters.DnsIpv4 = deviceEnvironment.EnvironmentSettings.DnsIpv4;
                o.TestParameters.DnsIpv6 = deviceEnvironment.EnvironmentSettings.DnsIpv6;
                o.TestParameters.NtpIpv4 = deviceEnvironment.EnvironmentSettings.NtpIpv4;
                o.TestParameters.NtpIpv6 = deviceEnvironment.EnvironmentSettings.NtpIpv6;
            }
            if (deviceEnvironment.Credentials != null)
            {
                o.TestParameters.UserName = deviceEnvironment.Credentials.UserName;
                o.TestParameters.Password = deviceEnvironment.Credentials.Password;
            }
            if (deviceEnvironment.Timeouts != null)
            {
                o.TestParameters.MessageTimeout = deviceEnvironment.Timeouts.Message;
                o.TestParameters.RebootTimeout = deviceEnvironment.Timeouts.Reboot;
                o.TestParameters.TimeBetweenTests = deviceEnvironment.Timeouts.InterTests;
            }
            if (deviceEnvironment.TestSettings != null)
            {
                o.TestParameters.EventTopic = deviceEnvironment.TestSettings.EventTopic;
                o.TestParameters.TopicNamespaces = deviceEnvironment.TestSettings.TopicNamespaces;
                o.TestParameters.OperationDelay = deviceEnvironment.TestSettings.OperationDelay;
                o.TestParameters.Password1 = deviceEnvironment.TestSettings.Password1;
                o.TestParameters.Password2 = deviceEnvironment.TestSettings.Password2;
                o.TestParameters.PTZNodeToken = deviceEnvironment.TestSettings.PTZNodeToken;
                o.TestParameters.RelayOutputDelayTime = deviceEnvironment.TestSettings.RelayOutputDelayTimeMonostable;
                o.TestParameters.SecureMethod = deviceEnvironment.TestSettings.SecureMethod;
                o.TestParameters.SubscriptionTimeout = deviceEnvironment.TestSettings.SubscriptionTimeout;
                o.TestParameters.TimeBetweenRequests = deviceEnvironment.TestSettings.RecoveryDelay;
                o.TestParameters.UseEmbeddedPassword = deviceEnvironment.TestSettings.UseEmbeddedPassword;
                o.TestParameters.VideoSourceToken = deviceEnvironment.TestSettings.VideoSourceToken;
                o.TestParameters.SearchTimeout = deviceEnvironment.TestSettings.SearchTimeout;
                o.TestParameters.RecordingToken = deviceEnvironment.TestSettings.RecordingToken;
                o.TestParameters.MetadataFilter = deviceEnvironment.TestSettings.MetadataFilter;
            }
            XmlSerializer serializer = new XmlSerializer(typeof(SerializableTestingParameters));
            FileStream stream = null;
            try
            {
                stream = new FileStream(fileName, FileMode.OpenOrCreate);
                serializer.Serialize((Stream) stream, o);
            }
            catch (Exception exception)
            {
                base.View.ShowError(exception);
                return;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
            if (base.View.OpenFileForEditing)
            {
                try
                {
                    ProcessStartInfo startInfo = new ProcessStartInfo("Notepad.exe", fileName) {
                        CreateNoWindow = false,
                        UseShellExecute = true
                    };
                    Process.Start(startInfo);
                }
                catch (Exception exception2)
                {
                    base.View.ShowError(exception2);
                }
            }
        }

        public TestTool.GUI.Data.Profile SaveCurrentProfile(string profileName)
        {
            TestTool.GUI.Data.Profile item = (from p in this._profiles.Profiles
                where p.Name == profileName
                select p).FirstOrDefault<TestTool.GUI.Data.Profile>();
            if (item == null)
            {
                item = new TestTool.GUI.Data.Profile(profileName);
                this._profiles.Profiles.Add(item);
            }
            item.InterTests = base.View.SettingsView.TimeBetweenTests;
            item.Reboot = base.View.SettingsView.RebootTimeout;
            item.Message = base.View.SettingsView.MessageTimeout;
            item.OperationDelay = base.View.SettingsView.OperationDelay;
            item.RecoveryDelay = base.View.SettingsView.RecoveryDelay;
            item.DnsIpv4 = base.View.SettingsView.DnsIpv4;
            item.NtpIpv4 = base.View.SettingsView.NtpIpv4;
            item.GatewayIpv4 = base.View.SettingsView.GatewayIpv4;
            item.DnsIpv6 = base.View.SettingsView.DnsIpv6;
            item.NtpIpv6 = base.View.SettingsView.NtpIpv6;
            item.GatewayIpv6 = base.View.SettingsView.GatewayIpv6;
            item.UseEmbeddedPassword = base.View.SettingsView.UseEmbeddedPassword;
            item.Password1 = base.View.SettingsView.Password1;
            item.Password2 = base.View.SettingsView.Password2;
            item.SecureMethod = base.View.SettingsView.SecureMethod;
            item.PTZNodeToken = base.View.SettingsView.PTZNodeToken;
            item.VideoSourceToken = base.View.SettingsView.VideoSourceToken;
            item.EventTopic = base.View.SettingsView.EventTopic;
            item.TopicNamespaces = base.View.SettingsView.TopicNamespaces;
            item.SubscriptionTimeout = base.View.SettingsView.SubscriptionTimeout;
            item.RelayOutputDelayTime = base.View.SettingsView.RelayOutputDelayTimeMonostable;
            TestOptions testOptions = ContextController.GetTestOptions();
            item.TestCases = testOptions.Tests;
            item.TestGroups = testOptions.Groups;
            item.AdvancedSettings = this.GetAdvancedSettings();
            ProfilesManager.Save(this._profiles);
            return item;
        }

        public List<string> SecureOperations()
        {
            if (this._operations == null)
            {
                this._operations = new List<string>();
                this._operations.Add("GetDeviceInformation");
                this._operations.Add("GetScopes");
                this._operations.Add("GetDiscoveryMode");
                this._operations.Add("GetUsers");
                this._operations.Add("GetDNS");
                this._operations.Add("GetNTP");
                this._operations.Add("GetNetworkInterfaces");
                this._operations.Add("GetNetworkProtocols");
                this._operations.Add("GetNetworkDefaultGateway");
                this._operations.Add("GetZeroConfiguration");
            }
            return this._operations;
        }

        public void ShowSettings(List<string> settings)
        {
            base.View.SettingsView.ShowMissingSettings("EventTopic");
        }

        public override void UpdateContext()
        {
            DeviceEnvironment deviceEnvironment = ContextController.GetDeviceEnvironment();
            deviceEnvironment.Timeouts = new Timeouts();
            deviceEnvironment.Timeouts.InterTests = base.View.SettingsView.TimeBetweenTests;
            deviceEnvironment.Timeouts.Message = base.View.SettingsView.MessageTimeout;
            deviceEnvironment.Timeouts.Reboot = base.View.SettingsView.RebootTimeout;
            deviceEnvironment.EnvironmentSettings = new EnvironmentSettings();
            deviceEnvironment.EnvironmentSettings.DnsIpv4 = base.View.SettingsView.DnsIpv4;
            deviceEnvironment.EnvironmentSettings.NtpIpv4 = base.View.SettingsView.NtpIpv4;
            deviceEnvironment.EnvironmentSettings.DnsIpv6 = base.View.SettingsView.DnsIpv6;
            deviceEnvironment.EnvironmentSettings.NtpIpv6 = base.View.SettingsView.NtpIpv6;
            deviceEnvironment.EnvironmentSettings.GatewayIpv4 = base.View.SettingsView.GatewayIpv4;
            deviceEnvironment.EnvironmentSettings.GatewayIpv6 = base.View.SettingsView.GatewayIpv6;
            deviceEnvironment.TestSettings = new TestSettings();
            deviceEnvironment.TestSettings.PTZNodeToken = base.View.SettingsView.PTZNodeToken;
            deviceEnvironment.TestSettings.UseEmbeddedPassword = base.View.SettingsView.UseEmbeddedPassword;
            deviceEnvironment.TestSettings.Password1 = base.View.SettingsView.Password1;
            deviceEnvironment.TestSettings.Password2 = base.View.SettingsView.Password2;
            deviceEnvironment.TestSettings.OperationDelay = base.View.SettingsView.OperationDelay;
            deviceEnvironment.TestSettings.RecoveryDelay = base.View.SettingsView.RecoveryDelay;
            deviceEnvironment.TestSettings.VideoSourceToken = base.View.SettingsView.VideoSourceToken;
            deviceEnvironment.TestSettings.SecureMethod = base.View.SettingsView.SecureMethod;
            deviceEnvironment.TestSettings.SubscriptionTimeout = base.View.SettingsView.SubscriptionTimeout;
            deviceEnvironment.TestSettings.EventTopic = base.View.SettingsView.EventTopic;
            deviceEnvironment.TestSettings.TopicNamespaces = base.View.SettingsView.TopicNamespaces;
            deviceEnvironment.TestSettings.RelayOutputDelayTimeMonostable = base.View.SettingsView.RelayOutputDelayTimeMonostable;
            deviceEnvironment.TestSettings.RecordingToken = base.View.SettingsView.RecordingToken;
            deviceEnvironment.TestSettings.SearchTimeout = base.View.SettingsView.SearchTimeout;
            deviceEnvironment.TestSettings.MetadataFilter = base.View.SettingsView.MetadataFilter;
            deviceEnvironment.TestSettings.RetentionTime = base.View.SettingsView.RetentionTime;
            deviceEnvironment.TestSettings.RawAdvancedSettings = this.GetAdvancedSettings();
            List<object> advancedSettings = base.View.SettingsView.AdvancedSettings;
            deviceEnvironment.TestSettings.AdvancedSettings = advancedSettings;
        }

        public override void UpdateView()
        {
            DeviceEnvironment deviceEnvironment = ContextController.GetDeviceEnvironment();
            base.View.SettingsView.TimeBetweenTests = deviceEnvironment.Timeouts.InterTests;
            base.View.SettingsView.MessageTimeout = deviceEnvironment.Timeouts.Message;
            base.View.SettingsView.RebootTimeout = deviceEnvironment.Timeouts.Reboot;
            base.View.SettingsView.OperationDelay = deviceEnvironment.TestSettings.OperationDelay;
            base.View.SettingsView.RecoveryDelay = deviceEnvironment.TestSettings.RecoveryDelay;
            base.View.SettingsView.DnsIpv4 = deviceEnvironment.EnvironmentSettings.DnsIpv4;
            base.View.SettingsView.DnsIpv6 = deviceEnvironment.EnvironmentSettings.DnsIpv6;
            base.View.SettingsView.NtpIpv4 = deviceEnvironment.EnvironmentSettings.NtpIpv4;
            base.View.SettingsView.NtpIpv6 = deviceEnvironment.EnvironmentSettings.NtpIpv6;
            base.View.SettingsView.GatewayIpv4 = deviceEnvironment.EnvironmentSettings.GatewayIpv4;
            base.View.SettingsView.GatewayIpv6 = deviceEnvironment.EnvironmentSettings.GatewayIpv6;
            base.View.SettingsView.UseEmbeddedPassword = deviceEnvironment.TestSettings.UseEmbeddedPassword;
            base.View.SettingsView.Password1 = deviceEnvironment.TestSettings.Password1;
            base.View.SettingsView.Password2 = deviceEnvironment.TestSettings.Password2;
            base.View.SettingsView.SecureMethod = deviceEnvironment.TestSettings.SecureMethod;
            base.View.SettingsView.PTZNodeToken = deviceEnvironment.TestSettings.PTZNodeToken;
            base.View.SettingsView.SubscriptionTimeout = deviceEnvironment.TestSettings.SubscriptionTimeout;
            base.View.SettingsView.EventTopic = deviceEnvironment.TestSettings.EventTopic;
            base.View.SettingsView.TopicNamespaces = deviceEnvironment.TestSettings.TopicNamespaces;
            base.View.SettingsView.RelayOutputDelayTimeMonostable = deviceEnvironment.TestSettings.RelayOutputDelayTimeMonostable;
            base.View.SettingsView.RecordingToken = deviceEnvironment.TestSettings.RecordingToken;
            base.View.SettingsView.SearchTimeout = deviceEnvironment.TestSettings.SearchTimeout;
            base.View.SettingsView.MetadataFilter = deviceEnvironment.TestSettings.MetadataFilter;
            base.View.SettingsView.RetentionTime = deviceEnvironment.TestSettings.RetentionTime;
            this.DisplayAdvancedSettings(deviceEnvironment.TestSettings.RawAdvancedSettings);
        }

        public List<TestTool.GUI.Data.Profile> Profiles
        {
            get
            {
                return this._profiles.Profiles;
            }
        }
    }
}

